﻿

var IntialactivityItems = function ($item, title, cls) {
    var str = "<ul class='sortable toolbox_ul li_style'>";
    var child
    child = "<li class='ui-state-default milestone_designtoolbox start' milestonetype=><img src='images/流程开始.png'class='absmiddle' />&nbsp;&nbsp;流程开始</li>";
    str = str + child;
    child = "<li class='ui-state-default milestone_designtoolbox end'><img src='images/流程结束.png'class='absmiddle' />&nbsp;&nbsp;流程结束</li>";
    str = str + child;
    child = "<li class='ui-state-default activity_designtoolbox content1'><img src='images/审核环节.png'class='absmiddle' />&nbsp;&nbsp;审核环节</li>";
    str = str + child;
    child = "<li class='ui-state-default activity_designtoolbox content2'><img src='images/审批环节.png'class='absmiddle' />&nbsp;&nbsp;审批环节</li>";
    str = str + child;
    child = "<li class='ui-state-default activity_designtoolbox content3'><img src='images/知会环节.png'class='absmiddle' />&nbsp;&nbsp;知会环节</li>";
    str = str + child;
    child = "<li class='ui-state-default activity_designtoolbox content4'><img src='images/执行环节.png'class='absmiddle' />&nbsp;&nbsp;执行环节</li>";
    str = str + child;
    child = "<li class='ui-state-default activity_designtoolbox content5'><img src='images/ewewe_095.png'class='absmiddle' />&nbsp;&nbsp;董事会黑盒</li>";
    str = str + "</ul>";
    var $str = $(str).appendTo($item);
    $str.find('img').css('background', 'url("images/bg.editor.li.1.jpg") no-repeat scroll left top transparent')
    .css('border-bottom', ' 1px solid #CBCBCB').css('padding','10px');
    //     background: url("../images/bg.editor.li.1.jpg") no-repeat scroll left top transparent;
    //    border-bottom: 1px solid #CBCBCB;
    //    padding: 10px;
    IntialItemHoverStyle($item);
};


var IntialItemHoverStyle = function ($item) {
    $item.find('.ui-state-default').each(function () {
        var $li = $(this);
        $li.mouseover(function () {
            $(this).hover(function () {
                $li.addClass('ui-state-highlight');
                $li.removeClass('ui-state-default');
            },
            function () {
                $li.addClass('ui-state-default');
                $li.removeClass('ui-state-highlight');
            });
        });

    });
};

var InitialDesignToolbox = function ($designToolbox) {
    $designToolbox
    //    .append('<p class="conditionDesignTitle"><a href="#">待选条件</a></p><div id="divConditionItems"></div>')
    .append('<div id="divActivityItems" class="pageEditorLeftPanelBoxList"></div>')
    //.append('<div id="divOthersItems"></div>')

    //var $conditionItems = $("#divConditionItems");
    var $activityItems = $("#divActivityItems");
    //var $othersItems = $("#divOthersItems");
    //IntialconditionItems($conditionItems, "待选条件", "conditionItem");
    IntialactivityItems($activityItems, "待选环节", "activityItem");
    //IntialothersItems($othersItems, "环节容器", "othersItems");
    $("#container").setoffwrite();


    $(".sortable").sortable({ helper: "clone",
        appendTo: "body", connectWith: ".sortableOtherItem"
        , over: function (event, ui) {

        },
        receive: function (event, ui) {
            $("#container").droppable('enable');
        }
    })
    $(".sortableOtherItem").sortable({ helper: function (event) {
        return $("<img src='/images/okay.png' style='width:60px'/>");
    },
        appendTo: "body"
    });
    $(".sortable").disableSelection();

    $(".sortable").sortable({ helper: "clone", appendTo: "body", connectWith: ".container_othersItems .sortable" });
    $(".sortable").disableSelection();

    
    //画布响应drop 事件 
    $("#container").droppable({
        accept: 'li',
        drop: function (event, ui) {
            var $container = $("#container");
            var position = $container.offset();
            x = event.pageX - position.left;
            y = event.pageY - position.top;
            var $draggable = $(ui.draggable);
            $draggable.unbind('hover');
            var c;
            var $addedDiv
            if ($draggable.hasClass('activity_designtoolbox')) {
                c = { wrapper: 'pageEditorElementItem', container: 'pageEditorElementItemBox' }
                $addedDiv = $('<div class="' + c.wrapper + '"><div class="' + c.container + '"></div></div>').appendTo($container);
            }
            else if ($draggable.hasClass('milestone_designtoolbox start')) {
                c = { wrapper: 'pageEditorElementMilestone', container: 'pageEditorElementMilestoneBox' }
                $addedDiv = $('<div class="' + c.wrapper + '"><div class="' + c.container + '"><div class="start"/></div></div>').appendTo($container);
            }
            else if ($draggable.hasClass('milestone_designtoolbox end')) {
                c = { wrapper: 'pageEditorElementMilestone', container: 'pageEditorElementMilestoneBox' }
                $addedDiv = $('<div class="' + c.wrapper + '"><div class="' + c.container + '"><div class="end"/></div></div>').appendTo($container);
            }
            //$addedDiv.addbox();
            $addedDiv.data('containerclass', c);
            $addedDiv.css('position', 'absolute').css('left', x).css('top', y);
            $addedDiv.addInterfaces();
            $addedDiv.clickFunction({ isnodeclick: 0, isarrowclick: 0 });
            $addedDiv.draggable({ drag: function () {
                $addedDiv.setdragFunction();
            }
            });
            $addedDiv.mouseup(function () {
                $addedDiv.setdragFunction();

            });
            $addedDiv.mouseover(function () {
                $addedDiv.css("cursor", "pointer")
            })
            $addedDiv.mouseout(function () {
                $addedDiv.css("cursor", "default")
            })
        }
    });

    
};



//点击环节删除按钮
var raiseActivityDelete = function ($target, $childDiv) {
    if (confirm('确定要删除次子项？')) {
        $target.remove();
        updateInterfacePosition($childDiv);
    }
}


//function deleteitems($addedDiv) {
//    if (confirm('确定要删除此项？')) {
//        $addedDiv.remove();
//        for (i = 0; i < linenum; i++) {

//            $("#container").find(".line" + i + "").each(function () {
//                var $this = $(this);
//                px = $this.attr("startx");
//                py = $this.attr("endx");

//            });
//            if (px == $addedDiv.get(0).id || py == $addedDiv.get(0).id) {
//                $(".line" + i + "").remove();
//            }

//        }
//    }

//}